import React, { useState } from 'react';
import { Form, Input, Button, Card, Typography, message } from 'antd';
import { LockOutlined, SafetyOutlined, MailOutlined } from '@ant-design/icons';
import { Link, useNavigate } from 'react-router-dom';
import { resetPassword } from '../api/auth';

const { Title, Text } = Typography;

const ResetPassword: React.FC = () => {
  const [loading, setLoading] = useState(false);
  const navigate = useNavigate();

  const onFinish = async (values: { code: string; newPassword: string }) => {
    setLoading(true);
    try {
      const response = await resetPassword({
        code: values.code,
        newPassword: values.newPassword
      });
      if (response.code === 200) {
        message.success('密码重置成功，请使用新密码登录');
        navigate('/login');
      } else {
        message.error(response.message || '重置失败');
      }
    } catch (error) {
      message.error('重置失败，请稍后重试');
    } finally {
      setLoading(false);
    }
  };

  return (
    <div style={{ 
      minHeight: '100vh', 
      display: 'flex', 
      alignItems: 'center', 
      justifyContent: 'center',
      background: 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)'
    }}>
      <Card style={{ width: 400, boxShadow: '0 4px 12px rgba(0,0,0,0.15)' }}>
        <div style={{ textAlign: 'center', marginBottom: 24 }}>
          <Title level={2} style={{ color: '#1890ff', marginBottom: 8 }}>
            重置密码
          </Title>
          <Text type="secondary">请输入邮箱中收到的验证码和新密码</Text>
        </div>

        <Form
          name="resetPassword"
          onFinish={onFinish}
          autoComplete="off"
          size="large"
        >
          <Form.Item
            name="code"
            rules={[
              { required: true, message: '请输入验证码!' },
              { len: 6, message: '验证码应为6位数字!' }
            ]}
          >
            <Input
              prefix={<MailOutlined />}
              placeholder="邮箱验证码"
              maxLength={6}
            />
          </Form.Item>

          <Form.Item
            name="newPassword"
            rules={[
              { required: true, message: '请输入新密码!' },
              { min: 6, message: '密码至少6个字符!' }
            ]}
          >
            <Input.Password
              prefix={<LockOutlined />}
              placeholder="新密码"
            />
          </Form.Item>

          <Form.Item
            name="confirmPassword"
            dependencies={['newPassword']}
            rules={[
              { required: true, message: '请确认新密码!' },
              ({ getFieldValue }) => ({
                validator(_, value) {
                  if (!value || getFieldValue('newPassword') === value) {
                    return Promise.resolve();
                  }
                  return Promise.reject(new Error('两次输入的密码不一致!'));
                },
              }),
            ]}
          >
            <Input.Password
              prefix={<SafetyOutlined />}
              placeholder="确认新密码"
            />
          </Form.Item>

          <Form.Item>
            <Button 
              type="primary" 
              htmlType="submit" 
              loading={loading}
              block
              style={{ height: 40 }}
            >
              重置密码
            </Button>
          </Form.Item>
        </Form>

        <div style={{ textAlign: 'center', marginTop: 16 }}>
          <Link to="/forgot-password" style={{ marginRight: 16 }}>
            <Text type="secondary">重新获取验证码</Text>
          </Link>
          <Link to="/login">
            <Text type="secondary">返回登录</Text>
          </Link>
        </div>
      </Card>
    </div>
  );
};

export default ResetPassword; 